//Hexamines
//(c) 2007 Felipe C.da S. Sanches <felipe.sanches@gmail.com>
// This is freesoftware under the terms of GNU General Public License version 3 or later
// See COPYING file for complete license terms.
//
//Cell Object:
IMAGE_CELL = "png/Cell.png";
IMAGE_VOID = "png/CellVoid.png";
IMAGE_1 = "png/CellOne.png";
IMAGE_2 = "png/CellTwo.png";
IMAGE_3 = "png/CellThree.png";
IMAGE_4 = "png/CellFour.png";
IMAGE_5 = "png/CellFive.png";
IMAGE_6 = "png/CellSix.png";
IMAGE_BOMB = "png/CellBomb.png";
IMAGE_BOMB_EXPLODED = "png/CellBombExploded.png";
IMAGE_FLAG = "png/CellFlag.png";
IMAGE_WRONG_FLAG = "png/CellWrongFlag.png";
IMAGE_SHADOW = "png/CellShadow.png";
IMAGE_NUMBER_BASE = "png/Number_";

CLOSED = 0;
OPENED = 1;
FLAG = 2;

function image_url(filename){
	return BASE_URL+filename;
}

function Cell(x,y){
	this.x=x;
	this.y=y;

	this.div = document.createElement("div");
	this.div.cell = this;

	this.image = document.createElement("img");
	this.div.appendChild(this.image);

	this.div.onclick = this.click;
	this.div.onmousedown = this.handle_mousedown;
	this.div.onmouseup = this.handle_mouseup;
	this.div.style.position = "absolute";
	this.div.style.left = 32*x + 16*(y%2);
	this.div.style.top = 28*y;
	this.init();
}

Cell.prototype.init = function(){
	this.status = CLOSED;
	this.image.src = image_url(IMAGE_CELL);
}

Cell.prototype.count_bombs = function(c){
	var result = 0;
	if (c.n1) result += c.n1.bomb;
	if (c.n2) result += c.n2.bomb;
	if (c.n3) result += c.n3.bomb;
	if (c.n4) result += c.n4.bomb;
	if (c.n5) result += c.n5.bomb;
	if (c.n6) result += c.n6.bomb;
	return result;
}


Cell.prototype.count_flags = function(c){
	var result = 0;
	if (c.n1 && c.n1.status==FLAG) result ++;
	if (c.n2 && c.n2.status==FLAG) result ++;
	if (c.n3 && c.n3.status==FLAG) result ++;
	if (c.n4 && c.n4.status==FLAG) result ++;
	if (c.n5 && c.n5.status==FLAG) result ++;
	if (c.n6 && c.n6.status==FLAG) result ++;
	return result;
}

Cell.prototype.toggle_flag = function(c){
	if (c.status != CLOSED && c.status != FLAG) return;

	if (c.status == CLOSED){
		c.image.src = image_url(IMAGE_FLAG);
		c.status = FLAG;
		c.board.n_of_flags++;
	} else {
		c.image.src = image_url(IMAGE_CELL);
		c.status = CLOSED;
		c.board.n_of_flags--;
	}
	this.board.counter.set_digits(this.board.n_of_bombs - this.board.n_of_flags);
	this.board.check_end_game();
}

Cell.prototype.handle_mousedown = function(e){
	if (this.cell.board.game_is_over) return;
	if (navigator.appName == 'Netscape'){
		switch (e.which){
		 case 2:
			this.cell.middle_mousedown(this.cell);
			break;
		 case 3:
			this.cell.toggle_flag(this.cell);
			return false;
		}
	}
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2){
		this.cell.toggle_flag(this.cell)
		return false;
	}
	//TO-DO: middleclick on internet explorer...
}

function shadow_cell(n, c){
	c.board.shadow_img[n-1].height=32;
	c.board.shadow_img[n-1].width=29;
	c.board.shadow_div[n-1].style.left = c.x*32 + (c.y%2)*16;	
	c.board.shadow_div[n-1].style.top = c.y*28;
}

function shadow_hide(c){
	for (var i=0;i<6;i++){
		c.board.shadow_img[i].height = 0;
		c.board.shadow_img[i].width = 0;
	}
}

Cell.prototype.middle_mousedown = function(c){
	c.board.multiclick=true;
	if (c.n1) shadow_cell(1, c.n1);
	if (c.n2) shadow_cell(2, c.n2);
	if (c.n3) shadow_cell(3, c.n3);
	if (c.n4) shadow_cell(4, c.n4);
	if (c.n5) shadow_cell(5, c.n5);
	if (c.n6) shadow_cell(6, c.n6);
}

Cell.prototype.handle_mouseup = function(e){
	shadow_hide(this.cell);
	if (this.cell.board.game_is_over) return;
	if (multiclick = true){
		this.cell.board.multiclick = false;
		this.cell.middleclick(this.cell);
	}
}

Cell.prototype.middleclick = function(c){
	//handle multiple-cell click

	if (c.board.game_is_over) return;
	if (c.status == FLAG||c.status == CLOSED) return;

	if ( c.count_flags(c) == c.count_bombs(c) ){
		if (c.n1 && c.n1.status == CLOSED) c.on_clicked(c.n1);
		if (c.n2 && c.n2.status == CLOSED) c.on_clicked(c.n2);
		if (c.n3 && c.n3.status == CLOSED) c.on_clicked(c.n3);
		if (c.n4 && c.n4.status == CLOSED) c.on_clicked(c.n4);
		if (c.n5 && c.n5.status == CLOSED) c.on_clicked(c.n5);
		if (c.n6 && c.n6.status == CLOSED) c.on_clicked(c.n6);
	}
}

Cell.prototype.click = function(e){
	if (this.cell.status != CLOSED) {
		this.cell.board.multiclick = true;
		this.cell.middleclick(this.cell);
	} else {
		this.cell.on_clicked(this.cell);
	}
	return true;
}

Cell.prototype.on_clicked = function(c){ 
	if (c.board.game_is_over) return;
	if (c.status == FLAG) return;
	if (c.bomb==1){
		c.image.src = image_url(IMAGE_BOMB_EXPLODED);
		c.bomb=0;//?
		c.board.gameover();
		return;
	}

	c.status = OPENED;
	c.board.n_of_open_cells++;

	switch (c.count_bombs(c)){ //dá pra usar this em vez de ter que passar c ?
		case 0:
			c.image.src = image_url(IMAGE_VOID);
			if (c.n1 && c.n1.status == CLOSED) c.on_clicked(c.n1);
			if (c.n2 && c.n2.status == CLOSED) c.on_clicked(c.n2);
			if (c.n3 && c.n3.status == CLOSED) c.on_clicked(c.n3);
			if (c.n4 && c.n4.status == CLOSED) c.on_clicked(c.n4);
			if (c.n5 && c.n5.status == CLOSED) c.on_clicked(c.n5);
			if (c.n6 && c.n6.status == CLOSED) c.on_clicked(c.n6);
			break;
		case 1: c.image.src = image_url(IMAGE_1); break;
		case 2: c.image.src = image_url(IMAGE_2); break;
		case 3: c.image.src = image_url(IMAGE_3); break;
		case 4: c.image.src = image_url(IMAGE_4); break;
		case 5: c.image.src = image_url(IMAGE_5); break;
		case 6: c.image.src = image_url(IMAGE_6); break;
	}
	this.board.check_end_game();
}

